<?php

namespace IDock\Insurance\CHAC\Response;

use IDock\Common\Tools\XMLTools;
use IDock\Http\Response;
use IDock\Printer\NonCar\NonCarInquirySinglePrinter;
use IDock\Printer\PremiumUnderwritingPrinter;
use IDock\Visitor\Premium\PremiumInquiryVisitor;

class EUnderwritingResponse extends AllCHACResponse
{

    /*** @var PremiumUnderwritingPrinter $printer */
    public $printer = PremiumUnderwritingPrinter::class;

    /*** @var PremiumInquiryVisitor $visitor */
    public $visitor;

    public function resolve(): Response
    {
        $data = $this->getBody();

        $this->result = $data;

        preg_match('/<return>(.*)<\/return>/', $data, $matches);

        $body = $matches[0] ?? [];

        if (!$body) {
            $this->setError('数据格式错误');
        }

        $body = XMLTools::XmlToArray($body, ['Kind'])['return'] ?? [];

        $this->setBody($body);

        $responseCode = $body['Head']['responseCode'] ?? '';
        $errorMessage = $body['Head']['errorMessage'] ?? '';

        if ($responseCode != '0000') {
            $this->setError($errorMessage);
            return $this;
        }

        if ($this->printer) {
            $this->printer = new $this->printer;
        }

        return $this;
    }

    public function printerInit(): Response
    {

        $proposalInfo = $this->get('Proposal', []);

        $underWriteInd = $proposalInfo['underWriteInd'] ?? '';

        $this->printer->inquiryNo = $proposalInfo['proposalNo'] ?? '';

        $this->printer->ciInquiryNo = $proposalInfo['compProposalNo'] ?? '';

        $this->printer->biInquiryNo = $proposalInfo['commProposalNo'] ?? '';

        if ($this->visitor->nonCarUnderwritingVisitor->getNonCarList()) {
            $this->printer->noCarUnderwritingPrinter->loadVisitor($this->visitor->nonCarUnderwritingVisitor->getNonCarList());

            /*** @var $nonCarInquirySinglePrinter NonCarInquirySinglePrinter */
            $nonCarInquirySinglePrinter = $this->printer->noCarUnderwritingPrinter->getNonCarInquiryList()[0];
            $nonCarInquirySinglePrinter->inquiryNo = $proposalInfo['noCarProposalNo'] ?? '';
        }

        //通过
        $this->printer->statusCode = self::statusCode($underWriteInd);

        return $this;
    }

    public static function statusCode($underWriteInd): string
    {
        //核保通过
        if (in_array($underWriteInd, ['1', '3', '5'])) {
            return 'U1';
        }
        //下发修改
        if ($underWriteInd == '2') {
            return 'U2';
        }

        //人工
        if (in_array($underWriteInd, ['8', '9', 'A'])) {
            return 'U3';
        }

        //失败
        if ($underWriteInd == '7') {
            return 'U4';
        }

        //拒保
        if ($underWriteInd == '4') {
            return 'U6';
        }

        //已承保
        if ($underWriteInd == '6') {
            return 'U5';
        }

        //待审核
        if (in_array($underWriteInd, ['0', ''])) {
            return 'U7';
        }

        return '';
    }
}
